import { MenuService } from './menu.service';
import { Injectable } from '@angular/core';
import { HttpCacheService } from '@core/net/http-cache.service';
import { ICurrentRole } from '@model/login';
import { USER_AUTH } from '@common/static';

@Injectable({
	providedIn: 'root'
})
export class AuthService {

	constructor(
		private httpCacheService: HttpCacheService
	) { }
	saveAuth(roles: ICurrentRole[]) {
		const plantsCode = [];
		if (roles?.length > 0) {
			roles.forEach(role => {
				const _operations = role.secOperations.map(sec => sec.operation);
				_operations.forEach(secAuth => {
					if (!plantsCode.includes(secAuth)) {
						plantsCode.push(secAuth);
					}
				});
			});
		}
		this.httpCacheService.saveSessionCache(USER_AUTH, JSON.stringify(plantsCode));
	}
	getAuth() {
		const menu = this.httpCacheService.getSessionCache(USER_AUTH);
		if (menu) {
			return JSON.parse(menu);
		}
		return [];
	}


	
}
